package com.eveinfo.display.asset;

import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
import java.util.HashSet;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.View.OnClickListener;
import android.view.View.OnLongClickListener;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;

import com.eveinfo.R;
import com.eveinfo.api.asset.ApiAsset;
import com.eveinfo.sql.DataBaseHelper;
import com.eveinfo.sql.data.Item;
import com.eveinfo.tools.ActivityLauncher;
import com.eveinfo.tools.EveInfoPreferences;
import com.eveinfo.tools.Tools;

public class ItemAdapter extends BaseAdapter {

	private Context context;

	private final Item item;
	private final ArrayList<Integer> locations;

	private Integer[] unfold;

	public ItemAdapter(Context context, int textViewResourceId, ListView listView, Item item) {
		super();
		this.context = context;
		this.item = item;
		locations = new ArrayList<Integer>(item.getAssets().keySet());
		final int size = locations.size();
		this.unfold = new Integer[size];
		for (int i = 0; i < size; i++) {
			unfold[i] = 0;
		}
		listView.setAdapter(this);
	}

	@Override
	public int getCount() {
		int total = 0;
		final ArrayList<Integer> locations = this.locations;
		int size = locations.size();
		for (int i = 0; i < size; i++) {
			total++;
			if (unfold[i] == 1) {
				Integer location = locations.get(i);
				ArrayList<ApiAsset> assets = item.getAssets().get(location);
				int assetsSize = assets.size();
				HashSet<Integer> set = new HashSet<Integer>();
				for (int j = 0; j < assetsSize; j++) {
					final ApiAsset parent = assets.get(j).getParent();
					if (parent == null) {
						set.add(0);
					} else {
						set.add(parent.getItemID());
					}
				}
				total += set.size();
			}
		}
		return total;
	}

	@Override
	public Object getItem(int position) {
		return position;
	}

	@Override
	public long getItemId(int position) {
		return position;
	}

	class Row {
		ApiAsset asset;
		long quantity;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parentView) {
		View view = convertView;

		LayoutInflater vi = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);

		int sectionnum = 0;
		int pos = position;
		int i = 0;
		for (int location : locations) {
			final int y = i;
			int sectionSize = 1;

			HashMap<Integer, Row> rows = new HashMap<Integer, Row>();
			if (unfold[i] == 1) {
				final ArrayList<ApiAsset> assets = item.getAssets().get(location);
				int size = assets.size();
				for (int j = 0; j < size; j++) {
					final ApiAsset asset = assets.get(j);
					final ApiAsset parent = asset.getParent();
					if (parent == null) {
						final int itemID = asset.getItemID();
						Row row = rows.get(itemID);
						if (row == null) {
							row = new Row();
							row.asset = asset;
							row.quantity = 0;
						}
						row.quantity += asset.getQuantity();
						rows.put(itemID, row);
					} else {
						final int itemID = parent.getItemID();
						Row row = rows.get(itemID);
						if (row == null) {
							row = new Row();
							row.asset = parent;
							row.quantity = 0;
						}
						row.quantity += asset.getQuantity();
						rows.put(itemID, row);
					}
				}
				sectionSize += rows.size();
			}

			if (pos == 0) {
				view = vi.inflate(R.layout.itemr, null);

				TextView stationTV = (TextView) view.findViewById(R.id.TVItemRStation);
				TextView nameTV = (TextView) view.findViewById(R.id.TVItemRName);
				TextView numberTV = (TextView) view.findViewById(R.id.TVItemRNumber);

				DataBaseHelper dataBaseHelper =  new DataBaseHelper(context);
				dataBaseHelper.openDataBase();
				String stationName = dataBaseHelper.getStationName(location);
				dataBaseHelper.close();

				if (stationName == null) {
					stationName = "unKnown";
				}
				String[] split = stationName.split(" - ");
				if (split.length < 3) {
					stationTV.setText(stationName);
				} else {
					stationTV.setText(split[0] + " - " + split[1]);
					nameTV.setText(split[2]);
				}

				long quantity = 0;
				final ArrayList<ApiAsset> arrayList = item.getAssets().get(location);
				int size = arrayList.size();
				for (int j = 0; j < size; j++) {
					quantity += arrayList.get(j).getQuantity();
				}

				numberTV.setText("" + quantity);

				view.setOnClickListener(new OnClickListener() {
					@Override
					public void onClick(View v) {
						if (unfold[y] == 0) {
							unfold[y] = 1;
						} else {
							unfold[y] = 0;
						}
						notifyDataSetChanged();
					}
				});
				return view;
			}
			if (pos < sectionSize) {
				view = vi.inflate(R.layout.itemr2, null);

				ImageView pictureIV = (ImageView) view.findViewById(R.id.IVItemR2Picture);
				TextView nameTV = (TextView) view.findViewById(R.id.TVItemR2Name);
				TextView numberTV = (TextView) view.findViewById(R.id.TVItemR2Number);

				final Collection<Row> values = rows.values();
				Row[] tabRow = new Row[values.size()];
				tabRow = values.toArray(tabRow);

				final Row row = tabRow[pos - 1];
				numberTV.setText("" + row.quantity);

				final Item type = row.asset.getItem();
				nameTV.setText("" + type.getTypeName());

				pictureIV.setImageDrawable(Tools.getItemPicture(type, EveInfoPreferences.ASSETS_IMAGE_SIZE_32));

				view.setOnClickListener(new OnClickListener() {
					@Override
					public void onClick(View v) {
						if (type.getCategoryID() == 6) {
							ActivityLauncher.displayShip(context, row.asset.getItemID(), row.asset.getTypeID());
						}
					}
				});
				view.setOnLongClickListener(new OnLongClickListener() {
					@Override
					public boolean onLongClick(View v) {
						ActivityLauncher.displayItemInfo(context, row.asset.getTypeID());
						return true;
					}
				});
				
				return view;
			}

			pos -= sectionSize;
			sectionnum++;
			i++;
		}
		return view;
	}

	class Parent {
		int itemID;
		int typeID;

		public Parent(int itemID, int typeID) {
			this.itemID = itemID;
			this.typeID = typeID;
		}
	}
}
